package services

import (
	"github.com/spf13/cast"
	"regexp"
	"strings"
)

type PryLockRule struct {
}

// 前19个字符
func (r *PryLockRule) GetActionTime(str string) string {
	return str[:19]
}

// 玩家名称：正则匹配User:开头，以(符号结尾，的字符串
func (r *PryLockRule) GetPlayerName(str string) string {
	//分组匹配，匹配到第一个(符号就结束,在如上的字符中只匹配出‘服主徐迷儿’
	reg := regexp.MustCompile(`User:\s+([^\s]+)\s+\(`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}

// 玩家straemid：正则匹配 (5, 76561198996443023),以(任意字符，字符串)结尾中间的数字
func (r *PryLockRule) GetPlayerStreamId(str string) string {
	reg := regexp.MustCompile(`\(\d+,\s+(\d+)\)`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}

// 开锁区域一：正则匹配Location:开头后面的所有字符
// 区域：正则匹配Location:开头后面的所有字符
func (r *PryLockRule) getLoc(str string) string {
	return r.GetUnlockPosition(str)
}

// 开锁区域二： 空白分割，再以=分割，取第二个
func (r *PryLockRule) extractXY(str string) (string, string) {
	//str为X=-863920.500 Y=-727241.875 Z=2226.781形式
	//1、以空白分割
	strslice := strings.Split(str, " ")
	//2、再以=分割
	x := strings.Split(strslice[0], "=")[1]
	y := strings.Split(strslice[1], "=")[1]
	return x, y
}

// 开锁区域三：根据x和y的值判断
func (r *PryLockRule) getLockArea(x string, y string) string {
	//1、将x和y转换为float64
	xfloat64 := cast.ToFloat64(x)
	yfloat64 := cast.ToFloat64(y)
	if 599996.750 > xfloat64 && xfloat64 > 904795.125 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && -295198.375 > yfloat64 && yfloat64 > -599996.750 {
		return "A4"
	}
	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B1"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 9600.000 > yfloat64 && yfloat64 > -295198.375 {
		return "B4"
	}
	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 314398.375 > yfloat64 && yfloat64 > 9600.000 {
		return "C4"
	}

	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && 619196.750 > yfloat64 && yfloat64 > 314398.375 {
		return "D4"
	}

	if -599996.750 > xfloat64 && xfloat64 > -904795.125 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z0"
	}
	if -295198.375 > xfloat64 && xfloat64 > -599996.750 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z1"
	}
	if 9600.000 > xfloat64 && xfloat64 > -295198.375 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z2"
	}
	if 314398.375 > xfloat64 && xfloat64 > 9600.000 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z3"
	}
	if 619196.750 > xfloat64 && xfloat64 > 314398.375 && -599996.750 > yfloat64 && yfloat64 > -904795.125 {
		return "Z4"
	}
	//2、判断x和y的范围
	//3、返回对应的区域
	return ""
}

// 原文
func (r *PryLockRule) GetOriginTxt(str string) string {
	//[LogMinigame] [BP_BombDefusalMinigame_C] User: Ying (227, 76561199258946074). Success: Yes. Elapsed time: 4.52. Failed attempts: 0. Target object: Mine_02_C_2142650995. User owner: 237([76561198441097923] 弥咕). Location: X=555684.812 Y=-409610.625 Z=2641.729
	reg := regexp.MustCompile(`\[LogMinigame]+\s+(.*)`)
	if reg.MatchString(str) {
		return reg.FindStringSubmatch(str)[1]
	}
	return ""
}

// 开锁区域方法
func (r *PryLockRule) GetLockArea(str string) string {
	//1、获取Location:后面的所有字符
	loc := r.getLoc(str)
	//2、将loc字符串分割为x和y
	//判断loc是否包含X=，如果不包含，说明是密码锁，直接返回
	if !strings.Contains(loc, "X=") {
		return ""
	}
	x, y := r.extractXY(loc)
	//3、根据x和y的值判断
	return r.getLockArea(x, y)
}

// 开锁结果 正则匹配Success: 开头.号结束的字符串
func (r *PryLockRule) GetLockResult(str string) string {
	//2023.11.27-00.11.43: [LogMinigame] [LockpickingMinigame_C] User: 服主徐迷儿 (13, 76561199081989219). Success: Yes. Elapsed time: 22.50. Failed attempts: 4. Target object: BP_Door_Upgr2_Single_C. Lock type: Advanced. User owner: 13([76561199081989219] 服主徐迷儿). Location: X=-864015.750 Y=-727988.812 Z=2215.359

	reg := regexp.MustCompile(`Success: (Yes|No)\.`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}

// 开锁次数 正则匹配Failed attempts: 开头的数字.号结束的字符串
func (r *PryLockRule) GetLockTimes(str string) int {
	//Failed attempts: 1.
	reg := regexp.MustCompile(`Failed attempts: (\d+)\.`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return -1
	}
	return cast.ToInt(strslice[1])
}

// 开锁耗时 正则匹配Elapsed time: 可能是小数点的数字.号结束的字符串
func (r *PryLockRule) GetElapsedTime(str string) float64 {
	reg := regexp.MustCompile(`Elapsed time: (\d+\.\d+)\.`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return -1
	}
	return cast.ToFloat64(strslice[1])
}

// 拥有者名字 User owner: 开头.号结束，中间的字符串中]符号与)符号之间的字符串
func (r *PryLockRule) GetOwnerName(str string) string {
	re := regexp.MustCompile(`User owner:\s*(-?\d+\(\[.+] (.+?)\)|N/A|-1\(\))`)
	match := re.FindStringSubmatch(str)

	if len(match) > 2 {
		username := match[2]
		if username == "" {
			return ""
		}
		return username
	}

	return ""
}

// 拥有者steamid,
func (r *PryLockRule) GetOwnerSteamid(str string) string {
	re := regexp.MustCompile(`User owner:\s*(\d+)\(\[(\d+)] .+?\)|N/A|-1\(\)`)
	match := re.FindStringSubmatch(str)

	if len(match) > 2 && match[2] != "" {
		userID := match[2]
		return userID
	}

	return ""
}

// 锁头级别 Lock type:开头.号结束，中间的字符串
func (r *PryLockRule) GetLockLevel(str string) string {
	reg := regexp.MustCompile(`Lock type:\s+(\w+)\.`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	level := reg.FindStringSubmatch(str)[1]
	if level == "Basic" {
		//Lock type: Basic. User owner:再次正则匹配
		reg = regexp.MustCompile(`Lock type: Basic. User owner:\s+(.*)\.\s+.*`)
		if reg.MatchString(str) {
			match := reg.FindStringSubmatch(str)
			if len(match) > 1 {
				owner := match[1]
				if owner == "N/A" {
					return "Rubbish"
				}
			}
		}
		return "Basic"
	}
	if level == "Medium" {
		return "Medium"
	}
	if level == "Advanced" {
		return "Advanced"
	}
	if level == "DialLock" {
		return "DialLock"
	}
	return ""
}

// 目标物品 Target object: 开头.号结束，中间的字符串
func (r *PryLockRule) GetTargetItem(str string) string {
	reg := regexp.MustCompile(`Target object:\s+(\w+)\.`)
	strslice := reg.FindStringSubmatch(str)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}

// 开锁位置Location: 后面的所有字符
func (r *PryLockRule) GetUnlockPosition(str string) string {
	//未避免出错，先将Dial Pad Location删除
	newStr := strings.Replace(str, "Dial Pad Location:", "", -1)
	reg := regexp.MustCompile(`Location:\s+(X=-?\d+\.\d+\s+Y=-?\d+\.\d+\s+Z=-?\d+\.\d+)`)
	strslice := reg.FindStringSubmatch(newStr)
	if len(strslice) == 0 {
		return ""
	}
	return strslice[1]
}
